package com.alliky.core.base.viewmodel;

import android.app.Application;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;

import com.alliky.core.R;
import com.alliky.core.base.BaseViewModel;
import com.alliky.core.binding.command.BindingCommand;
import com.alliky.core.data.BaseRepository;

/**
 * @Author: wxianing
 * @CreateDate: 2022/6/26 21:55
 * @Description: java类作用描述
 */
public class ToolbarViewModel<M extends BaseRepository> extends BaseViewModel<M> {
    //标题文字
    public ObservableField<String> titleText = new ObservableField<>("标题");
    //右边文字
    public ObservableField<String> rightText = new ObservableField<>("更多");

    public ObservableField<Drawable> rightIcon = new ObservableField<>(ContextCompat.getDrawable(getApplication(), R.mipmap.toolbar_more));

    public ObservableInt backVisibleObservable = new ObservableInt(View.VISIBLE);

    //右边文字的观察者
    public ObservableInt rightTextVisibleObservable = new ObservableInt(View.GONE);
    //右边图标的观察者
    public ObservableInt rightIconVisibleObservable = new ObservableInt(View.GONE);
    //兼容databinding，去泛型化
    public ToolbarViewModel toolbarViewModel;

    public ToolbarViewModel(@NonNull Application application) {
        super(application);
        toolbarViewModel = this;
    }

    /**
     * 设置标题
     *
     * @param text 标题文字
     */
    public void setTitleText(String text) {
        titleText.set(text);
    }

    /**
     * 设置右边文字
     *
     * @param text 右边文字
     */
    public void setRightText(String text) {
        rightText.set(text);
    }

    public void setBackIconVisible(int visibility) {
        backVisibleObservable.set(visibility);
    }

    /**
     * 设置右边文字的显示和隐藏
     *
     * @param visibility
     */
    public void setRightTextVisible(int visibility) {
        rightTextVisibleObservable.set(visibility);
    }

    /**
     * 设置右边图标的显示和隐藏
     *
     * @param visibility
     */
    public void setRightIconVisible(int visibility) {
        rightIconVisibleObservable.set(visibility);
    }

    public void setRightIcon(Drawable drawable) {
        rightIcon.set(drawable);
    }

    /**
     * 返回按钮的点击事件
     */
    public final BindingCommand backOnClick = new BindingCommand(() -> finish());

    public final BindingCommand rightTextOnClick = new BindingCommand(() -> rightTextOnClick());
    public final BindingCommand rightIconOnClick = new BindingCommand(() -> rightIconOnClick());

    /**
     * 右边文字的点击事件，子类可重写
     */
    protected void rightTextOnClick() {
    }

    /**
     * 右边图标的点击事件，子类可重写
     */
    protected void rightIconOnClick() {
    }
}
